<?php
namespace DAOZHEN\D;
class Reports extends \ZHMVC\D\DataBase
{
    public function getAllNum($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select count(*) as num from dz_reports";
                $data=$this->_db->getOne($sql);
            }
            else
            {
                //去掉第一个逻辑符号
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
            
                $sql="select count(*) as num from dz_reports where ".$tempS;
                $data=$this->_db->getOne($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select count(*) as num from dz_reports where ".$parameter;
            $data=$this->_db->getOne($sql);
        }
        else
        {
            $sql="select count(*) as num from dz_reports";
            $data=$this->_db->getOne($sql);
        }
        if(empty($data)==true)
        {
            $data["num"]=0;
        }
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
            
    public function getAll($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select * from dz_reports";
                $datas=$this->_db->getAll($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from dz_reports where ".$tempS;
                $datas=$this->_db->getAll($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from dz_reports where ".$parameter."";
            $datas=$this->_db->getAll($sql);
        }
        else
        {
            $sql="select * from dz_reports";
            $datas=$this->_db->getAll($sql);
        }
   
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
            
    public function getRows()
    {
        return $this->_rows;
    }
            
    public function getPages($limit,$parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            if($s=="")
            {
                $sql="select * from dz_reports order by id desc LIMIT ".$limit."";
                $datas=$this->_db->getAll($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from dz_reports where ".$tempS." order by id desc LIMIT ".$limit."";
                $datas=$this->_db->getAll($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from dz_reports where ".$parameter." order by id desc LIMIT ".$limit."";
            $datas=$this->_db->getAll($sql);
        }
        else
        {
            $sql="select * from dz_reports order by id desc LIMIT ".$limit."";
            $datas=$this->_db->getAll($sql);
        }
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
            
    public function getOne($postid)
    {
        $sql="select * from dz_reports where id=:id";
        $bind=array(":id"=>$postid);
        $data=$this->_db->getOne($sql,$bind);
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
            
    public function getOne1($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select * from dz_reports";
                $datas=$this->_db->getOne($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from dz_reports where ".$tempS;
                $datas=$this->_db->getOne($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from dz_reports where ".$parameter."";
            $datas=$this->_db->getOne($sql);
        }
        else
        {
            $sql="select * from dz_reports";
            $datas=$this->_db->getOne($sql);
        }
   
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
            
    public function add($report_id,$report_date,$total_guests,$avg_wait_time,$max_wait_time,$dept_load_rate)
    {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        $sql="insert into dz_reports (`report_id`,`report_date`,`total_guests`,`avg_wait_time`,`max_wait_time`,`dept_load_rate`) values (:report_id,:report_date,:total_guests,:avg_wait_time,:max_wait_time,:dept_load_rate)";
        $bind=array(":report_id"=>$report_id,":report_date"=>$report_date,":total_guests"=>$total_guests,":avg_wait_time"=>$avg_wait_time,":max_wait_time"=>$max_wait_time,":dept_load_rate"=>$dept_load_rate);
        $this->_db->update($sql,$bind);
        $this->_lastid=$id;
        return $this->_lastid;
    }
            
    public function update($report_id,$report_date,$total_guests,$avg_wait_time,$max_wait_time,$dept_load_rate)
    {
        $sql="update dz_reports set `report_id`=:report_id,`report_date`=:report_date,`total_guests`=:total_guests,`avg_wait_time`=:avg_wait_time,`max_wait_time`=:max_wait_time,`dept_load_rate`=:dept_load_rate where id=:id";
        $bind=array(":report_id"=>$report_id,":report_date"=>$report_date,":total_guests"=>$total_guests,":avg_wait_time"=>$avg_wait_time,":max_wait_time"=>$max_wait_time,":dept_load_rate"=>$dept_load_rate,":id"=>$id);
        $this->_db->update($sql,$bind);
        return 1;
    }
            
    public function delete($id)
    {
        $sql="delete from dz_reports where id=:id";
        $bind=array(":id"=>$id);
        $this->_db->update($sql,$bind);
        return 1;
    }

    public function addOne()
    {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        $sql = "insert into dz_reports(`id`) values (:id)";
        //echo $sql;
        $bind = array(":id" => $id);
        $this->_db->update($sql, $bind);
        $this->_lastid = $id;
        return 1;
    }
    
    public function getLastId(){
        return $this->_lastid;
    }
}